<template>
  <div>
    <div class="container">
      <div class="icon-top">
        <div class="back" @click="goBack">
          <van-icon color="#fff" name="arrow-left" />
        </div>
        <div class="share">
          <van-icon color="#fff" name="share" @click="showShare = true" />
        </div>
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
        />
      </div>
      <div class="goods">
        <div class="goods-img">
          <img :src="goodsDetail.imgUrl" />
        </div>
        <div class="goods-info">
          <div class="goods-title">{{ goodsDetail.goodsTitle }}</div>
          <div class="goods-des">{{ goodsDetail.des }}</div>
          <div class="tips" v-if="goodsDetail.tips">
            <div class="tips-title">【饮茶小贴士】</div>
            <div class="tips-item" v-for="(item, index) in goodsDetail.tips">
              {{ item.title }}
            </div>
          </div>
        </div>
      </div>
      <div class="goods-choose">
        <div
          class="choose-item"
          v-for="(item, index) in goodsDetail.goodsChoose"
          :key="index"
        >
          <div class="choose-title">{{ item.typeName }}</div>
          <div class="choose-all">
            <div
              class="choose-btn"
              :class="item2.isChecked ? 'ischeck' : ''"
              v-for="(item2, index) in item.chooseItem"
              :key="index"
              @click="ischecked(item.chooseItem, item2)"
            >
              {{ item2.title }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom-btn" v-show="!showShare">
      <div class="choose">
        <div class="choose-info">全糖，常温</div>
        <div class="red-or-add">
          <div class="btn-reduce" @click="numReduce">-</div>
          <div class="num">{{ goodsNum }}</div>
          <div class="btn-add" @click="numAdd">+</div>
        </div>
      </div>
      <div class="go-car" @click="goBuy">
        <i>¥</i><span>{{ goodsDetail.price * goodsNum }}</span
        >加入购物车
      </div>
    </div>
  </div>
</template>

<script>
import goodsDetail from "@/components/Goods/goods";
import { reqAddShopCart } from "@/api";
import { Toast } from 'vant';
export default {
  data() {
    return {
      goodsDetail:
        goodsDetail[this.$route.params.goodsTypeId - 1].goodsList[
          this.$route.params.goodsId - 1
        ],
      goodsNum: 1,
      showShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],
      arr: [],
    };
  },
  methods: {
    goBack() {
      this.$router.push("/buy");
    },
    numReduce() {
      if (this.goodsNum <= 1) {
        this.goodsNum = 1;
      } else {
        this.goodsNum -= 1;
      }
    },
    numAdd() {
      this.goodsNum += 1;
    },
    ischecked(chooseItem, item2) {
      chooseItem.forEach((item) => {
        item.isChecked = 0;
      });
      item2.isChecked = 1;
    },
    onSelect(option) {
      Toast(option.name);
      this.showShare = false;
    },
    async goBuy() {
      this.goodsDetail.goodsChoose.forEach((item) => {
        let { chooseItem } = item;
        chooseItem.forEach((i) => {
          if (i.isChecked === 1) {
            this.arr.push(i.title);
          }
        });
      });
      let chooseArr = Array.from(this.arr);

      let obj = {
        user_id: this.$store.state.login.userInfo.id,
        goods_name: this.goodsDetail.goodsTitle,
        goods_num: this.goodsNum,
        goods_price: this.goodsDetail.price,
        goods_choose: chooseArr.join(','),
        goods_img: this.goodsDetail.imgUrl
      };
      let res = await reqAddShopCart(obj);
      if (res.status === 200) {
        Toast('购物车添加成功');
        this.$router.push('/buy');
      } else {
        Toast('购物车添加失败');
      }
    },
  },
  mounted() {},
};
</script>

<style>
.van-share-sheet__options {
  justify-content: space-around;
}
</style>

<style lang="less" scoped>
.container {
  width: 100%;
  background-color: #fff;
  padding-bottom: 120px;
  box-sizing: border-box;
}

.icon-top {
  display: flex;
  position: fixed;
  top: 0;
  left: 0;
  margin: 10px;

  .back,
  .share {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.3);
    text-align: center;
    line-height: 30px;
    margin-right: 10px;
  }
}

.goods {
  .goods-img {
    width: 100%;
    height: 380px;
    text-align: center;
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 50px;

    img {
      height: 90%;
    }
  }

  .goods-info {
    padding: 0 20px 50px;

    .goods-title {
      font-size: 20px;
      font-weight: 450;
      margin-bottom: 30px;
    }

    .goods-des {
      font-size: 15px;
      font-weight: 350;
      line-height: 25px;
      margin-bottom: 20px;
    }

    .tips {
      color: #666666;
      font-size: 15px;

      .tips-title {
        font-weight: 500;
        margin-bottom: 15px;
      }

      .tips-item {
        line-height: 25px;
      }
    }
  }
}
.goods-choose {
  padding: 0 20px 40px 20px;
  .choose-item {
    .choose-title {
      font-size: 14px;
      font-weight: 400;
      margin: 20px 0;
    }
    .choose-all {
      display: flex;
      flex-wrap: wrap;
      .choose-btn {
        height: 30px;
        font-size: 15px;
        min-width: 70px;
        padding: 5px 10px;
        background-color: #eeeeee;
        color: #666666;
        border-radius: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 0 15px 15px 0;
      }
      .ischeck {
        background-color: #0151c6;
        color: #fff;
      }
    }
  }
}

.bottom-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  height: 120px;
  background-color: #fff;
  box-shadow: 0 1px 6px 2px rgba(0, 0, 0, 0.2);
  z-index: 10000;
  display: flex;
  flex-direction: column;
  justify-content: space-around;

  .choose {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .choose-info {
      color: #666666;
      font-size: 15px;
    }

    .red-or-add {
      width: 90px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .btn-reduce {
        width: 25px;
        height: 25px;
        font-size: 30px;
        font-weight: 300;
        border-radius: 50%;
        border: 1px solid #5a97f2;
        color: #0151c6;
        display: flex;
        justify-content: center;
        align-items: center;
        box-sizing: border-box;
        padding-bottom: 4px;
      }

      .num {
        font-size: 20px;
        font-weight: 500;
      }

      .btn-add {
        width: 25px;
        height: 25px;
        font-size: 20px;
        font-weight: 300;
        border-radius: 50%;
        background-color: #0151c6;
        color: #fff;
        display: flex;
        justify-content: center;
      }
    }
  }

  .go-car {
    width: 100%;
    height: 40px;
    border-radius: 30px;
    background-color: #0151c6;
    color: #fff;
    font-weight: 500;
    padding: 5px 0;
    display: flex;
    align-items: center;
    justify-content: center;

    i {
      font-size: 18px;
      font-weight: bold;
    }

    span {
      font-size: 23px;
      font-weight: bolder;
      margin: 0 5px;
    }
  }
}
</style>
